Python 程式語言具有用於處理文件的各種函數和語句。with語句和open()函數是這些語句和函數中的其中兩個。
模式 | 功能 |
---|---|
w(寫入模式) | 開啟檔案進行寫入。如果該檔案不存在,則會建立一個新檔案。後續的寫入操作將涵蓋現有內容。 |
r(讀取模式) | 開啟檔案進行讀取。 |
a(追加模式) | 開啟檔案進行追加。 |
w(寫入二進位模式) | 開啟檔案以二進位模式寫入。它處理二進位資料(例如圖像、音訊檔案或非文字檔案)。 |
with open("write.txt", 'w') as file:
file.write("Hello, world!")
執行結果:
>>done
write.txt:
Hello, world!
with open("write.txt", 'a') as file:
file.write("Hello, world!22")
print("done")
執行結果:
>>done
write.txt:
Hello, world!Hello, world!22
with open("write.txt", 'r') as file:
file_contents = file.read()
print(file_contents)
執行結果:
>>Hello, world!Hello, world!22
file_path = 'write.txt'
binary_string = "01001000 01100101 01101100 01101100 01101111 00101100 00100000 01110111 01101111 01110010 01101100 01100100 00100001" #Hello world 的二進制代碼
binary_string = binary_string.replace(" ", "")
binary_data = bytes.fromhex('%x' % int(binary_string, 2))
with open(file_path, 'wb') as file:
file.write(binary_data)
執行結果:
>>
write.txt
Hello, world!
Python Requests 是一個流行且功能強大的函式庫,用於在 Python 中發出 HTTP 請求。它提供了一個易於使用且直覺的介面,用於發送 HTTP/1.1 請求、處理各種類型的 HTTP 方法(例如 GET、POST、PUT、DELETE)以及管理請求標頭、參數、cookie 和身份驗證。
透過 Requests 函式庫,您可以與 API 互動、抓取網頁內容
開啟終端機(terminal)或命令提示字元(CMD)。
輸入下列指令
pip install requests
在終端機輸入下列指令
pip show requests
執行結果:
>>Name: requests
Version: 2.28.2
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
Author: Kenneth Reitz
Author-email: me@kennethreitz.org
License: Apache 2.0
Location: C:\Users\leung\AppData\Local\Programs\Python\Python311\Lib\site-packages
Requires: certifi, charset-normalizer, idna, urllib3
Required-by: chatbotAI, clarifai, clarifai-grpc, instapy, MeaningCloud-python, openai, pwntools, requests-oauthlib, tensorboard, torchvision, webdriverdownloader
from bs4 import BeautifulSoup
html_doc = """
<html><head><title>Hi</title></head>
<body>
<p>This is a test.</p>
<a id="link2" href="/my_link2">Link 2</a>
<p>Hello, <b class="boldtext">Bold Text</b></p>
</body></html>
"""
# 以 Beautiful Soup 解析 HTML 程式碼
soup = BeautifulSoup(html_doc, 'html.parser')
print(soup)
執行結果:
>><html><head><title>Hi</title></head>
<body>
<p>This is a test.</p>
<a href="/my_link2" id="link2">Link 2</a>
<p>Hello, <b class="boldtext">Bold Text</b></p>
</body></html>
網站: https://www2.pyc.edu.hk/index.php
from bs4 import BeautifulSoup
import requests as req
url ="https://www2.pyc.edu.hk/index.php"
response =req.get(url)
htmlfile = BeautifulSoup(response.text, 'html.parser')
# print(htmlfile.prettify())
print(htmlfile.title)
print(htmlfile.title.string)
if (response.status_code)==200:
print("ok")
執行結果:
>><title>Shatin Pui Ying College 沙田培英中學</title>
Shatin Pui Ying College 沙田培英中學
ok
import requests as req
url= "https://www2.pyc.edu.hk/img/indeximg/45th_SPYC-webbuttons_202223_english45.png"
Response =req.get(url)
photo =(Response.content)
with open(file="new.png",mode="wb")as f:
f.write(photo)
print("end")
執行結果:
>>end
試用python requests 取得下圖
import requests as req
from requests.models import Response
url= "https://lh3.googleusercontent.com/pw/AJFCJaUd_QKMd5y0Kg0oK-jCtHmQdwjMrSsRCDOc_5-Y_CPsGpwvx2L58rHe62MP1q6y_FODTqXiwX4mZCAnY8nRKRQdQuj2Lo_u9oWFQwPecjo9RkMqh6s3E8WDt7NnU__M8Z2ALcNkyJIdXCdfwXQt4an4=w1500-h3000"
Response =req.get(url)
photo =(Response.content)
with open(file="new.png",mode="wb")as f:
f.write(photo)
print("end")
圖片結果:
import requests as req
from bs4 import BeautifulSoup
url ="https://hk.yahoo.com/"
response = req.get(url)
htmlfile = BeautifulSoup(response.text, 'html.parser')
print("ok")
print(htmlfile.title.string)
spanalltag=(htmlfile.find_all("p")) #搜尋為p tag的字串
for i in spanalltag:
i=i.string
print(i)
執行結果:
很長的字串
這天就先介介紹requests,如果覺得我的文章對你有幫助或有更好的建議,可以追蹤我,可以按讚和不妨在留言區提出,明天再見吧。bye
reference:
https://medium.com/@chensheep1005/python-%E9%96%8B%E5%95%9Fbrowser-540dae3ad344
https://www.freecodecamp.org/chinese/news/with-open-in-python-with-statement-syntax-example/